/**
 * *********************** ******* ***********************************
 *
 *  
 * 
 *  
 * *******************************************************************
 */

package com.arges.web.domain.tableutil;

import java.util.Collection;
import java.util.List;

import org.springframework.util.StringUtils;

import com.arges.web.common.SQLPatchUtil;

/**
 * 用于给表删除数据, 仅支持一个字段的查询删除
 *
 * 
 * @version Ver 3.0
 * @since Ver 3.0
 * @Date 2012-12-19
 *
 */
@SuppressWarnings("unchecked")
public class DbTableForDataDelete
{

	private String tableName;

	private String columnName;

	private List values;

	private String deleteType;

	private Collection columnNames;

	boolean isSingleColumn = true;

	boolean isAppendPercent = true;

	public DbTableForDataDelete(String tableName, Collection columnNames, List values)
	{
		this.tableName = tableName;
		this.columnNames = columnNames;
		this.values = values;
		this.isSingleColumn = false;
	}

	public DbTableForDataDelete(String tableName, String columnName, List values)
	{
		this.tableName = tableName;
		this.columnName = columnName;
		this.values = values;
	}

	public DbTableForDataDelete(String tableName, String columnName, List values, String deleteType)
	{
		this.tableName = tableName;
		this.columnName = columnName;
		this.values = values;
		this.deleteType = deleteType;
	}

	public DbTableForDataDelete(String tableName, String columnName, List values, String deleteType,
			boolean isAppendPercent)
	{
		this.tableName = tableName;
		this.columnName = columnName;
		this.values = values;
		this.deleteType = deleteType;
		this.isAppendPercent = isAppendPercent;
	}

	public String toSql()
	{
		StringBuilder sql = new StringBuilder("");
		if (!isSingleColumn && (columnNames == null || columnNames.size() == 0))
		{
			return sql.toString();
		}
		sql.append("delete from ").append(tableName).append(" where ");
		if (!StringUtils.hasLength(deleteType))
		{
			if (isSingleColumn)
			{
				sql.append(SQLPatchUtil.createInConditionSql(columnName, values));
			}
			else
			{
				sql.append(" ( ");
				for (Object column : columnNames)
				{
					sql.append(" ");
					sql.append(SQLPatchUtil.createInConditionSql(String.valueOf(column), values));
					sql.append(" or");
				}
				sql = new StringBuilder(sql.substring(0, sql.length() - 2));
				sql.append(" )");
			}

		}
		else
		{
			if (!values.isEmpty())
			{
				int count = 0;
				for (Object str : values)
				{
					if (count != 0)
					{
						sql.append(" or ");
					}
					sql.append(columnName).append(" like ").append("'").append(String.valueOf(str));
					if (isAppendPercent)
					{
						sql.append("%'");
					}
					else
					{
						sql.append("'");
					}
					count++;
				}
			}
		}
		return sql.toString();
	}

	public void addData(Object data)
	{
	}

	public boolean hasData()
	{
		return !values.isEmpty();
	}

	public boolean isToSingleSql()
	{
		return true;
	}

	public List<String> toSqlList()
	{
		return null;
	}
}
